// 引入ali-oss
import OSS from 'ali-oss';
import { showToast } from '@/utils/message.js';
const user = JSON.parse(localStorage.getItem('userinfo'));
const vip = JSON.parse(localStorage.getItem('vipinfo'));
//console.log('user', user);
let client = new OSS({
  region: 'oss-cn-beijing', // bucket所在的区域， 默认oss-cn-hangzhou
  secure: true, // secure: 配合region使用，如果指定了secure为true，则使用HTTPS访问
  accessKeyId: 'LTAI5tEWaDYDTp4jHEcMJp6n', // 通过阿里云控制台创建的AccessKey
  accessKeySecret: 'bOiUcmUwJHYC8sFU8mLVxERTDTiNAn', // 通过阿里云控制台创建的AccessSecret
  bucket: 'ecodeuserstore', // 通过控制台或PutBucket创建的bucket
});
export const fileput = async (ObjName, fileUrl) => {
  try {
    let result = await client.put(`${ObjName}`, fileUrl);
    // ObjName为文件名字,可以只写名字，就直接储存在 bucket 的根路径，如需放在文件夹下面直接在文件名前面加上文件夹名称
    return result;
  } catch (e) {
    console.log(e);
  }
};
export function ossDownloadFile(objectKey) {
  let url = client.signatureUrl(objectKey, {
    // response: {
    //   'content-disposition':
    //     'attachment; filename="' + encodeURIComponent(objectKey.split('/')[objectKey.split('/').length - 1]) + '"',
    // },
    expires: 3600,
  });
  return url;
}
export const type = ref('');
function getFileName(name) {
  return name.substring(name.lastIndexOf('.'));
}
export function beforeUploadVideoOss(file) {
  console.log(file);
  type.value = '视频';
  // 限制上传类型
  //const fileExtensions = getFileName(file.name) === '.doc' || getFileName(file.name) === '.docx' || getFileName(file.name) === '.pdf' || getFileName(file.name) === '.xlsx' || getFileName(file.name) === '.zip'
  const fileExtensions =
    getFileName(file.name) === '.mp4' ||
    getFileName(file.name) === '.mov' ||
    getFileName(file.name) === '.avi' ||
    getFileName(file.name) === '.wmv' ||
    getFileName(file.name) === '.mkv' ||
    getFileName(file.name) === '.flv';
  //限制的上限为500Mb
  //const maxSize = file.size / 1024 / 1024 < 5;
  const maxSize = file.size <= vip.limit2Audiovideoone;
  console.log(fileExtensions);
  if (!fileExtensions) {
    showToast('上传文件类型只能是 .mp4, .mov, .avi, .wmv, .mkv, .flv 格式!', 'error');
  }
  if (!maxSize) {
    showToast('上传文件大小不能超过 ' + vip.limit2Audiovideoone / 1024 / 1024 + 'MB!', 'error');
  }
  return fileExtensions && maxSize;
}
export function beforeUploadAuidoOss(file) {
  console.log(file);
  type.value = '音频';
  // 限制上传类型
  //const fileExtensions = getFileName(file.name) === '.doc' || getFileName(file.name) === '.docx' || getFileName(file.name) === '.pdf' || getFileName(file.name) === '.xlsx' || getFileName(file.name) === '.zip'
  const fileExtensions =
    getFileName(file.name) === '.mp3' ||
    getFileName(file.name) === '.wav' ||
    getFileName(file.name) === '.aac' ||
    getFileName(file.name) === '.ogg' ||
    getFileName(file.name) === '.flac';
  //限制的上限为500Mb
  //const maxSize = file.size / 1024 / 1024 < 5;
  const maxSize = file.size <= vip.limit2Audiovideoone;
  console.log(fileExtensions);
  if (!fileExtensions) {
    showToast('上传文件类型只能是 .mp3, .wav, .aac, .ogg, .flac 格式!', 'error');
  }
  if (!maxSize) {
    showToast('上传文件大小不能超过 ' + vip.limit2Audiovideoone / 1024 / 1024 + 'MB!', 'error');
  }
  return fileExtensions && maxSize;
}
export function beforeUploadImageOss(file) {
  console.log(file);
  type.value = '图片';
  // 限制上传类型
  //const fileExtensions = getFileName(file.name) === '.doc' || getFileName(file.name) === '.docx' || getFileName(file.name) === '.pdf' || getFileName(file.name) === '.xlsx' || getFileName(file.name) === '.zip'
  const fileExtensions =
    getFileName(file.name) === '.jpg' ||
    getFileName(file.name) === '.jpeg' ||
    getFileName(file.name) === '.png' ||
    getFileName(file.name) === '.gif' ||
    getFileName(file.name) === '.bmp' ||
    getFileName(file.name) === '.tiff';
  //限制的上限为500Mb
  //const maxSize = file.size / 1024 / 1024 < 5;
  const maxSize = file.size <= vip.limit2Imgtxtone;
  console.log(fileExtensions);
  if (!fileExtensions) {
    showToast('上传文件类型只能是 .jpg, .jpeg, .png, .gif,.bmp, .tiff 格式!', 'error');
  }
  if (!maxSize) {
    showToast('上传文件大小不能超过 ' + vip.limit2Imgtxtone / 1024 / 1024 + 'MB!', 'error');
  }
  return fileExtensions && maxSize;
}
export function beforeUploadICarouselOss(file) {
  console.log(file);
  type.value = '滚动';
  // 限制上传类型
  //const fileExtensions = getFileName(file.name) === '.doc' || getFileName(file.name) === '.docx' || getFileName(file.name) === '.pdf' || getFileName(file.name) === '.xlsx' || getFileName(file.name) === '.zip'
  const fileExtensions =
    getFileName(file.name) === '.jpg' ||
    getFileName(file.name) === '.jpeg' ||
    getFileName(file.name) === '.png' ||
    getFileName(file.name) === '.gif' ||
    getFileName(file.name) === '.bmp' ||
    getFileName(file.name) === '.tiff';
  //限制的上限为500Mb
  //const maxSize = file.size / 1024 / 1024 < 5;
  const maxSize = file.size <= vip.limit2Imgtxtone;
  console.log(fileExtensions);
  if (!fileExtensions) {
    showToast('上传文件类型只能是 .jpg, .jpeg, .png, .gif,.bmp, .tiff 格式!', 'error');
  }
  if (!maxSize) {
    showToast('上传文件大小不能超过 ' + vip.limit2Imgtxtone / 1024 / 1024 + 'MB!', 'error');
  }
  return fileExtensions && maxSize;
}
export function beforeUploadIFileOss(file) {
  console.log(file);
  type.value = '文件';
  // 限制上传类型
  const fileExtensions =
    getFileName(file.name) === '.doc' ||
    getFileName(file.name) === '.docx' ||
    getFileName(file.name) === '.pdf' ||
    getFileName(file.name) === '.xlsx' ||
    getFileName(file.name) === '.zip';

  //限制的上限为500Mb
  //const maxSize = file.size / 1024 / 1024 < 5;
  const maxSize = file.size <= vip.limit2Wordpdfone;
  console.log(fileExtensions);
  if (!fileExtensions) {
    showToast('上传文件类型只能是 .doc, .docx, .pdf, .xlsx, .zip 格式!', 'error');
  }
  if (!maxSize) {
    showToast('上传文件大小不能超过 ' + vip.limit2Wordpdfone / 1024 / 1024 + 'MB!', 'error');
  }
  return fileExtensions && maxSize;
}

export function formatBytes(bytes) {
  if (bytes === 0) {
    return '0 Bytes';
  }

  const k = 1024;
  const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];

  const i = Math.floor(Math.log(bytes) / Math.log(k));

  return `${Number.parseFloat((bytes / k ** i).toFixed(2))} ${sizes[i]}`;
}

